/**
 * Copyright &copy; 2012-2016 <dictdown href="https://github.com/thinkgem/jeesite">JeeSite</dictdown> All rights reserved.
 */
package org.spring.springboot.app.home.homepage.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advinfo.domain.vo.AdvInfoCountVO;
import org.spring.springboot.app.adv.advinfo.domain.vo.AdvInfoVO;
import org.spring.springboot.app.adv.advinfo.service.IAdvInfoService;
import org.spring.springboot.app.agent.balancelog.domain.po.SysOfficeBalanceLogPO;
import org.spring.springboot.app.agent.balancelog.domain.vo.SysOfficeBalanceLogSearchVO;
import org.spring.springboot.app.agent.balancelog.domain.vo.SysOfficeBalanceLogVO;
import org.spring.springboot.app.agent.balancelog.service.ISysOfficeBalanceLogService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.hard.hardlog.domain.vo.HardLogCountVO;
import org.spring.springboot.app.hard.hardlog.domain.vo.HardLogFlowVO;
import org.spring.springboot.app.hard.hardlog.domain.vo.HardLogNumberVO;
import org.spring.springboot.app.hard.hardlog.domain.vo.HardLogVO;
import org.spring.springboot.app.hard.hardlog.service.IHardLogService;
import org.spring.springboot.app.home.homepage.domain.vo.HomePageUsagerateVO;
import org.spring.springboot.app.home.homepage.domain.vo.HomePageVO;
import org.spring.springboot.app.platform.balancelog.service.IPlatformBalanceLogService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Api(description = "首页接口",tags = ApiIndex.HomePageController)
@RequestMapping(value = "/homepage")
@RestController
public class HomePageController {

	@Autowired
	private ISysOfficeBalanceLogService sysofficebalanceLogService;

	@Autowired
	private IPlatformBalanceLogService platformbalanceLogService;

	@Autowired
	private IHardLogService hardLogService;

	@Autowired
	private IAdvInfoService advinfoService;

	@ApiOperation(value = "查询昨天收支明细字典类型")
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@RequestMapping(value = "/yesterdayplatformbalanceLogType", method = RequestMethod.GET)
	public R<HomePageVO> findType(
			@ApiParam(value = "类型", required = false) @RequestParam(defaultValue = "") String officeType,
			@ApiIgnore User user
	) {
		R<HomePageVO> result = new R<HomePageVO>();
		HomePageVO hp=new HomePageVO();
		String i =null;
		if(officeType.equals("1") || officeType.equals("2") || officeType.equals("4")){
				i=sysofficebalanceLogService.yesterdaySum(user.getOfficeId());
		}else if(officeType.equals("5")){
			i=platformbalanceLogService.yesterdaySum();
		}
		if(i==null){
			hp.setSum("0");
			result.setData(hp);
		}else{
			hp.setSum(i);
			result.setData(hp);
		}
		return result;
	}

	@ApiOperation(value = "查询上周收支明细字典类型")
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@RequestMapping(value = "/lastweekplatformbalanceLogType", method = RequestMethod.GET)
	public R<HomePageVO> findTypelastweek(
			@ApiParam(value = "类型", required = false) @RequestParam(defaultValue = "") String officeType,
			@ApiIgnore User user
	) {
		R<HomePageVO> result = new R<HomePageVO>();
		HomePageVO hp=new HomePageVO();
		String i =null;
		if(officeType.equals("1") || officeType.equals("2") || officeType.equals("4")){
			i=sysofficebalanceLogService.lastweekSum(user.getOfficeId());
		}else if(officeType.equals("5")){
			i=platformbalanceLogService.lastweekSum();
		}
		if(i==null){
			hp.setSum("0");
			result.setData(hp);
		}else{
			hp.setSum(i);
			result.setData(hp);
		}
		return result;
	}

	@ApiOperation(value = "查询上月收支明细字典类型")
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@RequestMapping(value = "/lastmonthplatformbalanceLogType", method = RequestMethod.GET)
	public R<HomePageVO> findTypelastmonth(
			@ApiParam(value = "类型", required = false) @RequestParam(defaultValue = "") String officeType,
			@ApiIgnore User user
	) {
		R<HomePageVO> result = new R<HomePageVO>();
		HomePageVO hp=new HomePageVO();
		String i =null;
		if(officeType.equals("1") || officeType.equals("2") || officeType.equals("4")){
			i=sysofficebalanceLogService.lastmonthSum(user.getOfficeId());
		}else if(officeType.equals("5")){
			i=platformbalanceLogService.lastmonthSum();
		}
		if(i==null){
			hp.setSum("0");
			result.setData(hp);
		}else{
			hp.setSum(i);
			result.setData(hp);
		}
		return result;
	}

	@ApiOperation(value = "查询柱状图")
	@RequestMapping(value = "/column", method = RequestMethod.GET)
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@AccessTokenVerify
	public R<List<HardLogVO>> hardlogGraphical(
			@ApiIgnore User user
	){
		boolean one=false;
		boolean two=false;
		boolean three=false;
		boolean four=false;
		boolean five=false;
		boolean six=false;
		boolean serven=false;
		R result = new R();
		List<HardLogVO> _list = hardLogService.findGraphical(user.getOfficeId());
		List<HardLogCountVO> list = hardLogService.findCount(user.getOfficeId());

		//循环判断有那几个星期
		for(HardLogCountVO hc:list){
			if(hc.getDay().equals(2)){
				one=true;  //周一
			}else if(hc.getDay().equals(3)){
				two=true;  //周二
			}else if(hc.getDay().equals(4)){
				three=true;  //周三
			}else if(hc.getDay().equals(5)){
				four=true;  //周四
			}else if(hc.getDay().equals(6)){
				five=true;  //周五
			}else if(hc.getDay().equals(7)){
				six=true;  //周六
			}else if(hc.getDay().equals(1)){
				serven=true;  //周日
			}
		}

		/*******************************/
		//给星期为空的一条数据
		for(int i=0;i<7-list.size();i++){
			HardLogVO hv=new HardLogVO();
			if(one==false){
				hv.setTime("2");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				one=true;
			}else if(two==false){
				hv.setTime("3");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				two=true;
			}else if(three==false){
				hv.setTime("4");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				three=true;
			}else if(four==false){
				hv.setTime("5");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				four=true;
			}else if(five==false){
				hv.setTime("6");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				five=true;
			}else if(six==false){
				hv.setTime("7");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				six=true;
			}else if(serven==false){
				hv.setTime("1");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				serven=true;
			}
		}


		//判断类型
		for(HardLogVO ho:_list){
			if(ho.getCity().equals("1")){
				ho.setCity("tokyo");
			}else if(ho.getCity().equals("2")){
				ho.setCity("newYork");
			}else if(ho.getCity().equals("11")){
				ho.setCity("berlin");
			}else if(ho.getCity().equals("12")){
				ho.setCity("beijing");
			}else if(ho.getCity().equals("13")){
				ho.setCity("red");
			}else if(ho.getCity().equals("14")){
				ho.setCity("green");
			}
		}

		//转换为中式周末
		for(HardLogVO ho:_list){
			if(ho.getTime().equals("2")){
				ho.setTime("1");
			}else if(ho.getTime().equals("3")){
				ho.setTime("2");
			}else if(ho.getTime().equals("4")){
				ho.setTime("3");
			}else if(ho.getTime().equals("5")){
				ho.setTime("4");
			}else if(ho.getTime().equals("6")){
				ho.setTime("5");
			}else if(ho.getTime().equals("7")){
				ho.setTime("6");
			}else if(ho.getTime().equals("1")){
				ho.setTime("7");
			}
		}
		//周一到周日排序
		Collections.sort(_list,new Comparator<HardLogVO>(){
			@Override
			public int compare(HardLogVO o1,HardLogVO o2) {
				return Integer.valueOf(o1.getTime())- Integer.valueOf(o2.getTime());
			}
		});

		for(HardLogVO ho:_list){
			if(ho.getTime().equals("1")){
				ho.setTime("周一");
			}else if(ho.getTime().equals("2")){
				ho.setTime("周二");
			}else if(ho.getTime().equals("3")){
				ho.setTime("周三");
			}else if(ho.getTime().equals("4")){
				ho.setTime("周四");
			}else if(ho.getTime().equals("5")){
				ho.setTime("周五");
			}else if(ho.getTime().equals("6")){
				ho.setTime("周六");
			}else if(ho.getTime().equals("7")){
				ho.setTime("周日");
			}
		}
		result.setData(_list);
		return result;
	}

	@ApiOperation(value = "查询曲线图")
	@RequestMapping(value = "/curve", method = RequestMethod.GET)
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@AccessTokenVerify
	public R<List<AdvInfoVO>> advinfoGraphical(
			@ApiIgnore User user
	){
		boolean one=false;
		boolean two=false;
		boolean three=false;
		boolean four=false;
		boolean five=false;
		boolean six=false;
		boolean serven=false;
		R result = new R();
		List<AdvInfoVO> _list = advinfoService.findGraphical(user.getOfficeId());
		List<AdvInfoCountVO> list = advinfoService.findCount(user.getOfficeId());

		//循环判断有那几个星期
		for(AdvInfoCountVO hc:list){
			if(hc.getDay().equals(2)){
				one=true;  //周一
			}else if(hc.getDay().equals(3)){
				two=true;  //周二
			}else if(hc.getDay().equals(4)){
				three=true;  //周三
			}else if(hc.getDay().equals(5)){
				four=true;  //周四
			}else if(hc.getDay().equals(6)){
				five=true;  //周五
			}else if(hc.getDay().equals(7)){
				six=true;  //周六
			}else if(hc.getDay().equals(1)){
				serven=true;  //周日
			}
		}

		/*******************************/
		//给星期为空的一条数据
		for(int i=0;i<7-list.size();i++){
			AdvInfoVO hv=new AdvInfoVO();
			if(one==false){
				hv.setTime("2");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				one=true;
			}else if(two==false){
				hv.setTime("3");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				two=true;
			}else if(three==false){
				hv.setTime("4");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				three=true;
			}else if(four==false){
				hv.setTime("5");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				four=true;
			}else if(five==false){
				hv.setTime("6");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				five=true;
			}else if(six==false){
				hv.setTime("7");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				six=true;
			}else if(serven==false){
				hv.setTime("1");
				hv.setTem(0);
				hv.setCity("red");
				_list.add(hv);
				serven=true;
			}
		}


		//判断类型
		for(AdvInfoVO ho:_list){
			if(ho.getCity().equals("1")){
				ho.setCity("tokyo");
			}else if(ho.getCity().equals("2")){
				ho.setCity("newYork");
			}else if(ho.getCity().equals("3")){
				ho.setCity("berlin");
			}else if(ho.getCity().equals("4")){
				ho.setCity("beijing");
			}
		}

		//转换为中式周末
		for(AdvInfoVO ho:_list){
			if(ho.getTime().equals("2")){
				ho.setTime("1");
			}else if(ho.getTime().equals("3")){
				ho.setTime("2");
			}else if(ho.getTime().equals("4")){
				ho.setTime("3");
			}else if(ho.getTime().equals("5")){
				ho.setTime("4");
			}else if(ho.getTime().equals("4")){
				ho.setTime("5");
			}else if(ho.getTime().equals("7")){
				ho.setTime("6");
			}else if(ho.getTime().equals("1")){
				ho.setTime("7");
			}
		}
		//周一到周日排序
		Collections.sort(_list,new Comparator<AdvInfoVO>(){
			@Override
			public int compare(AdvInfoVO o1,AdvInfoVO o2) {
				return Integer.valueOf(o1.getTime())- Integer.valueOf(o2.getTime());
			}
		});

		for(AdvInfoVO ho:_list){
			if(ho.getTime().equals("1")){
				ho.setTime("周一");
			}else if(ho.getTime().equals("2")){
				ho.setTime("周二");
			}else if(ho.getTime().equals("3")){
				ho.setTime("周三");
			}else if(ho.getTime().equals("4")){
				ho.setTime("周四");
			}else if(ho.getTime().equals("5")){
				ho.setTime("周五");
			}else if(ho.getTime().equals("6")){
				ho.setTime("周六");
			}else if(ho.getTime().equals("7")){
				ho.setTime("周日");
			}
		}
		result.setData(_list);
		return result;
	}




	@ApiOperation(value = "查询近30收益")
	@RequestMapping(value = "/earnings", method = RequestMethod.GET)
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	public R<List<SysOfficeBalanceLogVO>> findRecen(
			@ApiIgnore User user
	) {
		R result = new R();
		List<SysOfficeBalanceLogPO> _list = sysofficebalanceLogService.findRecen(user.getOfficeId());
		/*PageInfo p =new PageInfo(_list);
		p.setList(getVOS(p.getList()));*/
		result.setData(_list);
		return result;
	}



	@ApiOperation(value = "查询近7天开门使用率")
	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
	@RequestMapping(value = "/usagerate", method = RequestMethod.GET)
	public R<List<HomePageUsagerateVO>> findusagerate(
			@ApiIgnore User user
	) {
		R result = new R();

		List<HardLogNumberVO> number=hardLogService.TotalNumber(user.getOfficeId());  //总人数.
		List<HardLogFlowVO> flow=hardLogService.findFlow(user.getOfficeId());  //出入流
		List<HomePageUsagerateVO> list = new ArrayList<HomePageUsagerateVO>();

		HomePageUsagerateVO hp=new HomePageUsagerateVO();
		hp.setPointer("开门使用率");
		hp.setY(1);
		if(number!=null && flow!=null){
			if(number.get(0).getNumber()!="" && flow.size()!=0){
				double usagerate=flow.size()/Integer.parseInt(number.get(0).getNumber().toString());
				hp.setLength(usagerate);
				hp.setValue(usagerate);
				list.add(hp);
			}
		}else{
			hp.setLength(0);
			hp.setValue(0);
			list.add(hp);
		}
		result.setData(list);

		return result;
	}

}
